﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using SmartCity.Shared;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace CommonManagement
{
    /// <summary>
    /// 区域应用服务类
    /// </summary>
    public class AreaAppService : ApplicationService, IAreaAppService
    {
        private readonly IRepository<Area, int> _areaRepository;

        public AreaAppService(
            IRepository<Area, int> areaRepository)
        {
            _areaRepository = areaRepository;
        }

        // 列表
        public async Task<ResultResponse<IList<AreaDto>>> ListAsync(string provinceName, string cityName, string name = null)
        {
            // 查询
            var query = _areaRepository
                .Where(m => m.City.Province.Name.Equals(provinceName))
                .Where(m => m.City.Name.Equals(cityName))
                .WhereIf(!string.IsNullOrWhiteSpace(name), m => m.Name.Contains(name))
                .Include(m => m.City)
                .Include(m => m.City.Province);
            var list = await query.ToListAsync();
            // 返回结果
            var dtos = ObjectMapper.Map<List<Area>, List<AreaDto>>(list);
            return ResultResponse<IList<AreaDto>>.Successful(dtos);
        }
    }
}